<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Categories_model extends MY_Model {
	protected $categories;
	
	function __construct(){
		parent::__construct();
	}
	
	function table_name(){
		return 'categories';
	}
	
	
	function get_category($category_id){
		if (!isset($this->categories[$category_id])){
			$category = $this->get_categories(array('category_id'=>$category_id));
			$this->categories[$category_id] = $category;
		}else{
			$category = $this->categories[$category_id];
		}
		if (empty($category)){
			return false;
		}
		return $category[0];
	}
	
	function get_categories($filters = array(),$counting = false){
		if (isset($this->CI->cache)){
			$caching = true;
			$cache_key = 'ci_CATEGORIES_'.md5(serialize($filters));
			if ($counting){
				$cache_key = 'ci_CATEGORIES_COUNT_'.md5(serialize($filters));
			}
			if ($return = $this->cache->file->get($cache_key)) {
				return ($return == 'empty_cache') ? FALSE : $return;
			}
		}else{
			$caching = false;
		}
		$this->db->select('*');
		$this->db->from('categories');
		
		if (isset($filters['module'])){
			$this->db->where('module',$filters['module']);
		}
		if (isset($filters['category_id'])){
			$this->db->where('category_id',$filters['category_id']);
		}
		if (isset($filters['alias'])){
			$this->db->where('alias',$filters['alias']);
		}
		if (isset($filters['status'])){
			$this->db->where('status',$filters['status']);
		}
		if (isset($filters['other_category_id'])){
			$this->db->where('category_id !=',$filters['other_category_id']);
		}
		
		if (isset($filters['parent_id'])){
			$this->db->where('categories.parent_id',$filters['parent_id']);	
		}
		
		if (isset($filters['sort']) && isset($filters['dir'])){
			$this->db->order_by($filters['sort'],$filters['dir']);
		}
		if (isset($filters['limit'])) {
			$offset = (isset($filters['page'])) ? $filters['page'] : 0;
			$this->db->limit($filters['limit'], $offset);
		}
		$result = $this->db->get();
		if($counting !=false){
			$rows = $result->num_rows();
			$result->free_result();
			
			if ($caching == TRUE) {
				$this->CI->cache->file->save($cache_key, $rows, (5*60));	
			}
			
			return $rows;
		}
		if ($result->num_rows() == 0) {
			return false;
		}
		$categories = array();
		//$categories = $result->result_array();
		foreach ($result->result_array() as $category){
			$category['link'] = create_url($category['module'].'/'.$category['alias'],array('rewrite'=>true));
			$categories[] = $category;
		}
		if ($caching){
			$this->CI->cache->file->save($cache_key,$categories);
		}
		$result->free_result();
		return $categories;
	}
	
	function get_tree_categories($filters = array(),$counting = false, $treerecurse = true){
		$categories = $this->get_categories($filters,$counting);
		if ($counting){
			return $categories;
		}
		$children = array ();
		if (count($categories) > 0 && is_array($categories)){
			foreach ($categories as $category){
				$category['id'] = $category['category_id'];
				$pt = $category['parent_id'];
				$list = @$children[$pt]?$children[$pt]: array ();
				array_push($list,$category);
				$children[$pt] = $list;
			}
		}
		if ($treerecurse){
			$category_tree = treerecurse(0,'',array(), $children);
			return $category_tree;
		}else{
			if ($categories){
				return $categories[0];
			}
		}
		return false;
	}
	
	function get_category_by_alias($category_alias){
		$category = $this->get_categories(array('alias'=>$category_alias));
		if ($category) {
			return $category[0];
		}
		return false;
	}
	
	function insert_category($attributes=array()){
		return $this->save($attributes);
	}
	
	function update_category($id,$attributes){
		return $this->update($attributes,array('category_id'=>$id)) ;
	}

	function delete_category($category_id){
		return $this->delete(array('category_id'=>$category_id));
	}
	
	function change_status($cat_id, $change = 1) {
        return $this->update_status($change, array('category_id' => $cat_id));
    }
}
